package com.jingdianjichi.interview.server.rpc;



import com.jingdianjichi.auth.api.UserFeignService;
import com.jingdianjichi.auth.entity.AuthUserDTO;
import com.jingdianjichi.auth.entity.Result;

import com.jingdianjichi.interview.server.rpc.entity.UserInfo;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:UserRpc
 * Package:com.jingdianjichi.subject.infra.rpc
 * Description:
 *
 * @Author fst
 * @Create 2025/1/4 17:09
 * @Version 1.0
 */
@Component
public class UserRpc
{
    @Resource
    private UserFeignService userFeignService;

    public UserInfo getUserInfo(String userName)
    {
        AuthUserDTO authUserDTO =new AuthUserDTO();
        authUserDTO.setUserName(userName);
        Result<AuthUserDTO> result =userFeignService.getUserInfo(authUserDTO);

        UserInfo userInfo = new UserInfo();

        if(result.getSuccess())
        {
            userInfo = new UserInfo();
            userInfo.setUserName(result.getData().getUserName());
            userInfo.setNickName(result.getData().getNickName());
            userInfo.setAvatar(result.getData().getAvatar());
            return userInfo;
        }
        return userInfo;
    }

    public Map<String, UserInfo> batchGetUserInfo(List<String> userNameList)
    {
        if(CollectionUtils.isEmpty(userNameList))
        {
            return Collections.emptyMap();
        }

        Result<List<AuthUserDTO>> listResult =userFeignService.listByIds(userNameList);

        Map<String, UserInfo> result=new HashMap<>();

        for(AuthUserDTO data:listResult.getData())
        {
            UserInfo userInfo = new UserInfo();

            userInfo.setUserName(data.getUserName());

            userInfo.setAvatar(data.getAvatar());

            userInfo.setNickName(data.getNickName());

            result.put(data.getUserName(),userInfo);
        }

        return result;
    }
}
